<?php

class Controller {

    public static function exec() {
    	
        $class = Uri::getClass();
        $method = Uri::getMethod();

        $action = new $class;

        if(Secutiry::secure($class, $method)){
            $action->$method();
        }else{
        	$url = Secutiry::getPage($class, $method);
        	
        	// se for true redireciona para a URL configurada na classe
        	if($url){
        		header('location:' . Secutiry::getPage($class, $method));
        	}else{ // se n�o existir url configurada manda para a classe de serguranca
        		header('location: /home/seguranca');
        	}
        	
        }
    }

    /**
     * Renderiza o template, view, ou valor fornecido.
     * @param string $templateOrView Nome do template ou view.
     * @param array $bindings Array que contem os bindings para o template.
     */
    public static function render($templateOrView, $bindings = NULL, $return = FALSE) {

        if ($templateOrView === "json") {

            OutputFormatter::convert($bindings);

            return;
        }

        if ($bindings != NULL) {
            foreach ($bindings as $key => $value) {
                $$key = $value;
            }
        }

        $path = "app/view/" . $templateOrView . ".phtml";


        if (file_exists($path)) {
            ob_start();

            include $path;

            $templateOrView = ob_get_contents();

            ob_get_clean();
        }


        if ($bindings != NULL) {

            $mustache = new Mustache();

            if ($return) {
                return $mustache->render($templateOrView, $bindings);
            }
            echo $mustache->render($templateOrView, $bindings);
        } else {

            if ($return) {
                return $templateOrView;
            }

            echo $templateOrView;
        }
    }

}

?>